import baseView from '../../../../app/base/baseView';

const { ccclass, property } = cc._decorator;

interface callback {
    (obj: { tapIndex: number }): void
}

interface onShow { itemList?: string[], itemColor?: string, success?: callback, fail?: callback, complete?: callback }

@ccclass
export default class roofActionSheet extends baseView {
    @property(cc.Layout)
    private contentLay: cc.Layout = null;

    @property(cc.Node)
    private btns: cc.Node = null;

    @property(cc.Button)
    private cancelBtn: cc.Button = null;

    private success: callback = null;
    private fail: callback = null;
    private complete: callback = null;

    protected onLoad() {
        this.cancelBtn.node.on('click', this.cancel, this);
        this.node.on('touchend', this.cancel, this);
    }

    protected confirm(index: number) {
        this.hide();
        if (this.success) {
            this.success({ tapIndex: index });
        }
        if (this.complete) {
            this.complete({ tapIndex: index });
        }
    }

    protected cancel() {
        this.hide();
        if (this.fail) {
            this.fail({ tapIndex: -1 });
        }
        if (this.complete) {
            this.complete({ tapIndex: -1 });
        }
    }

    protected onShow({ itemList = [], itemColor = '#000000', success, fail, complete }: onShow = {}) {
        this.success = success;
        this.fail = fail;
        this.complete = complete;

        let color = cc.color().fromHEX(itemColor);

        this.btns.getComponent('initPrefab').init(itemList.length, itemList, (node: cc.Node, index: number) => {
            node.on('click', function () { this.confirm(index); }, this);

            const label = node.children[0].getComponent(cc.Label);
            label.node.color = color;
            label.string = itemList[index];
        });

        this.contentLay.node.stopAllActions();
        this.contentLay.updateLayout();
        this.contentLay.node.y = -cc.winSize.height / 2 - this.contentLay.node.height;
        this.contentLay.node.runAction(cc.moveTo(0.2, 0, -cc.winSize.height / 2));
    }

    protected onHide() {
        this.contentLay.node.stopAllActions();
    }
}
